package com.sino.util;

/****************************************************
 * 功能:为列表分页显示提供基础类
 * 公司:哈尔滨信诺数码
 * 作者:李志东
 * 时间:2006-3-15
 */
public class PageControl {
  int PageSize = 20; //一页显示的记录数
  int RowCount = 0; //记录总数
  int PageCount = 0; //总页数
  int Page = 1; //待显示页码
  //设置一页显示的记录数
  public void setPageSize(int PageSize) {
    this.PageSize = PageSize;
  }
  //获取一页显示的记录数
  public int getPageSize() {
    return PageSize;
  }
  //设置记录总数
  public void setRowCount(int RowCount) {
    this.RowCount = RowCount;
    this.PageCount = (this.RowCount + this.PageSize - 1) / this.PageSize;
    if (this.Page > this.PageCount)
	this.Page = this.PageCount;
  }
  //获取记录总数
  public int getRowCount() {
    return RowCount;
  }
  //设置总页数
  public void setPageCount(int PageCount) {
    this.PageCount = PageCount;
  }
  //获取总页数
  public int getPageCount() {
    return this.PageCount;
  }
  //设置显示的页码
  public void setPage(String Page) {
    if (Page == null) {
      this.Page = 1;
    } else
      if (Page.equals("")) {
          this.Page = 1;
      } else {
        this.Page = Integer.parseInt(Page);
        if (this.Page < 1)
          this.Page = 1;
      }
  }

  //获取当前页码
  public int getPage() {
    return Page;
  }
  //显示分页字符串
  public String getPageInfo(String action,String str) {
    StringBuffer sb = new StringBuffer();
    sb.append("<b>分页>></b>&nbsp;");
    if (Page > 1)
		sb.append("<a href='" + action + "&page=1" + str + "'>");
		sb.append("首页</a>&nbsp;");
    if (Page > 1)
		sb.append("<a href='" + action + "&page=" + Integer.toString(Page - 1) + str + "'>");
		sb.append("上页</a>&nbsp;");
    if (Page < PageCount)
		sb.append("<a href='" + action + "&page=" + Integer.toString(Page + 1) + str + "'>");
		sb.append("下页</a>&nbsp;");
    if (Page < PageCount)
		sb.append("<a href='" + action + "&page=" + Integer.toString(PageCount) + str + "'>");
		sb.append("尾页</a>&nbsp;页次：");
		sb.append("<b>"
              + Integer.toString(Page)
              + "</b>/<b>"
              + Integer.toString(PageCount)
              + "</b>页&nbsp;");
		sb.append("<b>" + Integer.toString(PageSize) + "</b>条记录/页&nbsp;");
		sb.append("共<b>"+ Integer.toString(RowCount) +"</b>条记录&nbsp;");
		sb.append("<font color=#0000FF>跳转>></font>");
		sb.append("<select name='yehao' onchange=\"javascript:location='" + action + "&page='+this.options[this.selectedIndex].value + '"
              + str
              + "'\">");
		for (int i = 1; i <= PageCount; i++) {
			if (i == Page)
				sb.append("<option value='" + i + "' selected>" + i + "</option>");
			else
				sb.append("<option value='" + i + "'>" + i + "</option>");
		}
		sb.append("</select>");
		return sb.toString();
  }

  public String getShortPageInfo(String str) {
	StringBuffer sb = new StringBuffer();
	sb.append("<b>分页>></b>&nbsp;");
	if (Page > 1)
	  sb.append("<a href='?page=1" + str + "'>");
	sb.append("首页</a>&nbsp;");
	if (Page > 1)
	  sb.append("<a href='?page=" + Integer.toString(Page - 1) + str + "'>");
	sb.append("上页</a>&nbsp;");
	if (Page < PageCount)
	  sb.append("<a href='?page=" + Integer.toString(Page + 1) + str + "'>");
	sb.append("下页</a>&nbsp;");
	if (Page < PageCount)
	  sb.append("<a href='?page=" + Integer.toString(PageCount) + str + "'>");
	sb.append("尾页</a>&nbsp;页次：");
	sb.append("<b>"
			  + Integer.toString(Page)
			  + "</b>/<b>"
			  + Integer.toString(PageCount)
			  + "</b>页&nbsp;");
	sb.append("共<b>"+ Integer.toString(RowCount) +"</b>条记录&nbsp;");
	return sb.toString();
  }
  public String getPageInfo() {
    return getPageInfo("","");
  }
  //获取记录指针定位到待显示页的第一条记录上
  public int getAbsolute() {
    if(Page == 0)
      return 1;
    return ((Page - 1) * PageSize + 1);
  }
  public static void main(String[] args) {
    PageControl p = new PageControl();
    p.setPageSize(4);
    p.setPage("2");
    p.setRowCount(20);
    String s = p.getPageInfo();
  }

  /**
   * 此处插入方法说明。
   * 创建日期：(2002-9-15 22:36:07)
   */
  public PageControl() {
    super();
    setPageSize(20);
  }

  /**
   * 此处插入方法说明。
   * 创建日期：(2002-9-15 22:39:16)
   */
  public void setPageType(int type) {
    if (type == 1) {
      PageSize = 20;
    } else
      if (type == 2) {
    PageSize = 20;
    }
  }
}

